package com.signway.utils;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2011-2018, by your Signway, All rights reserved.
 * -----------------------------------------------------------------
 *
 * ProjectName: Droid
 *
 * Author: Administrator
 *
 * Email: xing.zhu@Signway.cn
 *
 * Description: LogUtil工具类
 *
 * -----------------------------------------------------------------
 * 2018/8/28 0028 : Create LogUtil.java
 * -----------------------------------------------------------------
 */

import android.util.Log;


public class LogHelper {

    public static final String TAG = "DS";

    public static boolean showD = true;
    public static boolean showI = true;
    public static boolean showW = true;
    public static boolean showE = true;
    public static boolean showSync = true;
    private static LogHelper instance = new LogHelper();

    public static void initLogLevel(boolean showI, boolean showD, boolean showW, boolean showE){
        instance.showD = showD;
        instance.showI = showI;
        instance.showW = showW;
        instance.showE = showE;
    }

    private void logMsg(int type, String msg, String... key){
        if((key == null) || (key.length <= 0)){
            return;
        }

        StringBuffer str = new StringBuffer();
        int i = 1;
        while (i < key.length){
            str.append("[");
            str.append(key[i]);
            str.append("]");
            i++;
        }

        str.append(msg);

        switch (type){
            case 0:{
                Log.i(key[0], str.toString());
            }
            break;
            case 1:{
                Log.d(key[0], str.toString());
            }
            break;
            case 2:{
                Log.w(key[0], str.toString());
            }
            break;
            case 3:{
                Log.e(key[0], str.toString());
            }
            break;
        }
    }

    public static void logI(String msg, String... key){
        if (showI) {
            instance.logMsg(0, msg, key);
        }
    }

    public static void logD(String msg, String... key){
        if (showD) {
            instance.logMsg(1, msg, key);
        }
    }

    public static void logW(String msg, String... key){
        if (showW) {
            instance.logMsg(2, msg, key);
        }
    }

    public static void logE(String msg, String... key){
        if (showE) {
            instance.logMsg(3, msg, key);
        }
    }

    private String getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();
        if (sts == null) {
            return null;
        }
        for (StackTraceElement st : sts)
            if (!st.isNativeMethod()) {
                if (!st.getClassName().equals(Thread.class.getName())) {
                    if (!st.getClassName().equals(getClass().getName())) {
                        return "[" + Thread.currentThread().getName() + "(" + Thread.currentThread().getId() +
                                "):" + st.getFileName() + ":" + st.getLineNumber() + "]";
                    }
                }
            }
        return null;
    }

    private String createMessage(String msg) {
        String functionName = getFunctionName();
        return functionName + " - " + msg;
    }

    private void info(String tag, String msg) {
        String message = createMessage(msg);
        com.orhanobut.logger.Logger.i(message);
        Log.i(tag, message);
    }

    private void debug(String tag, String msg) {
        String message = createMessage(msg);
        com.orhanobut.logger.Logger.d(message);
        Log.d(tag, message);
    }

    private void error(String tag, String msg) {
        String message = createMessage(msg);
        com.orhanobut.logger.Logger.e(message);
        Log.e(tag, message);
    }


    public static void d(String msg) {
        if (showD) {
            instance.debug(TAG, msg);
        }
    }

    public static void d(String tag, String msg) {
        if (showD) {
            instance.debug(tag, msg);
        }
    }

    public static void i(String msg) {
        if (showI) {
            instance.info(TAG, msg);
        }
    }

    public static void i(String tag, String msg) {
        if (showI) {
            instance.info(tag, msg);
        }
    }

    public static void w(String msg) {
        if (showW) {
            instance.info(TAG, msg);
        }
    }

    public static void w(String tag, String msg) {
        if (showW) {
            instance.info(tag, msg);
        }
    }

    public static void e(String msg) {
        if (showE) {
            instance.error(TAG, msg);
        }
    }

    public static void e(String tag, String msg) {
        if (showE) {
            instance.error(tag, msg);
        }
    }

    public static void s(String msg) {
        if (showSync) {
            instance.error("syncLog", msg);
        }
    }

    public static void t(String msg) {
        if (showSync) {
            instance.error("correctTimeLog", msg);
        }
    }

    /**
     * 初始化日志等级
     * @param showI
     * @param showD
     * @param showW
     * @param showE
     * @param showSync
     */
    public static void initLogParams(boolean showI, boolean showD, boolean showW, boolean showE, boolean showSync){
        instance.showD = showD;
        instance.showI = showI;
        instance.showW = showW;
        instance.showE = showE;
        instance.showSync = showSync;
    }
}
